import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/assessments.dart';
import 'package:ichinesereader/controller/books.dart';
import 'package:ichinesereader/controller/reports.dart';
import 'package:ichinesereader/controller/schooladmin.dart';
import 'package:ichinesereader/controller/user.dart';
import 'package:ichinesereader/models/assessment/assessmentQueryDto.dart';
import 'package:ichinesereader/models/report/basketsResult.dart';
import 'package:ichinesereader/models/student/assignmentSearchDto.dart';
import 'package:ichinesereader/models/teacher/TeacherAssessment.dart';
import 'package:ichinesereader/models/teacher/TeacherClass.dart';
import 'package:ichinesereader/pages/teacher/assessmentProgress.dart';
import 'package:ichinesereader/pages/teacher/classSettings.dart';
import 'package:ichinesereader/pages/teacher/reportsAssessment.dart';
import 'package:ichinesereader/pages/teacher/reportsBasket.dart';
import 'package:ichinesereader/pages/teacher/reportsHomework.dart';
import 'package:ichinesereader/routes/routePages.dart';
import 'package:ichinesereader/themes/appTheme.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/dialogs/unFinishedStudentsDialog.dart';
import 'package:ichinesereader/widgets/grids/bookSLiderGridNoBar.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/widgets/layouts/appdrawerTeacher.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';
import 'package:ichinesereader/widgets/sidebars/settingsBarHorizontal.dart';
import 'package:universal_html/html.dart' as UHtml;
import '../../themes/pageStyles.dart';
import '../../widgets/sidebars/teacherSidebar.dart';

class TeacherHome extends StatefulWidget {
  static const String routeName = '/teacher/home';

  @override
  _TeacherHomeState createState() => _TeacherHomeState();
}

class _TeacherHomeState extends State<TeacherHome> {
  final BooksController _booksController = Get.find();
  final UserController _userController = Get.find();
  final ReportsController _reportsController = Get.put(ReportsController());
  final SchoolAdminController _schoolAdminController = Get.find();
  final AssessmentsController _assessmentsController = Get.find();
  final box = Hive.box('appconfig');
  final PageStyles _pageStyles = PageStyles();
  String _classId = '';
  var _classes = <TeacherClass>[];

  @override
  void initState() {
    int locale = box.get('locale', defaultValue: 0);
    bool isDark = box.get('theme', defaultValue: 0) == 1;

    _userController.fetchMyClasses().then((classes) {
      if (classes.isNotEmpty) {
        _classId = box.get(CacheManagerKey['currentclass'].toString()) ?? '';
        setState(() {
          _classes = classes;
        });

        Future.delayed(Duration(milliseconds: 10), () {
          if (_classId != '')
            _schoolAdminController.fetchClassDetailsNewByClassId(_classId);
          fetchAssignmentData(_classId);
          fetchAssessmentData(_classId);
        });
      }
    });
    Future.delayed(Duration(milliseconds: 100), () {
      if (_booksController.newBookItems.isEmpty) {
        _booksController.fetchNewBooksItems();
      }

      if (isDark) {
        Get.changeThemeMode(ThemeMode.dark);
      } else {
        Get.changeThemeMode(ThemeMode.light);
      }

      if (locale == 0) {
        Get.updateLocale(Locale('en', 'US'));
      }
      if (locale == 1) {
        Get.updateLocale(Locale('zh', 'CN'));
      }
      if (locale == 2) {
        Get.updateLocale(Locale('zhHant', 'TW'));
      }
    });

    super.initState();
  }

  @override
  void dispose() {
    // not delete it, otherwise redscreen when go to assessment report
    //Get.delete<ReportsController>();
    super.dispose();
  }

  void saveSharedCurrentClassId(String classId) async {
    box.put('nanhaiV4.8TeacherClassSelected', classId);
  }

  void fetchAssignmentData(classId) {
    AssignmentSearchDto dto = new AssignmentSearchDto(
        assignmentType: "ASSIGNMENT",
        live: true,
        past: false,
        sortField: "-created");
    _reportsController.fetchClassAssignmentsData(classId, dto);
  }

  void fetchAssessmentData(classId) {
    AssessmentQueryDto dto = new AssessmentQueryDto();
    dto.live = true;
    dto.queued = false;
    dto.past = false;
    _assessmentsController.fetchAssessmentsByClass(classId, dto);
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('start building teacher home page');
    final isSmallScreen =
        MediaQuery.of(context).size.width < Constants.DefinedScreenSmallSize;
    final isBigScreen =
        MediaQuery.of(context).size.width > Constants.DefinedScreenBigSize;
    int _selectedSidebarItem = RoutePages.teacherPageItems
        .indexWhere((page) => page.route == Get.currentRoute);

    return Scaffold(
      backgroundColor: Colors.transparent,
      appBar: isSmallScreen ? CustomAppBar(title: 'Home'.tr) : null,
      drawer: isSmallScreen ? TeacherAppDrawer() : null,
      body: Container(
        height: double.infinity,
        decoration: _pageStyles.pageBoxDecoration(),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Visibility(
                visible: !isSmallScreen,
                child: TeacherSidebarX(
                  selectedIndex: _selectedSidebarItem,
                  isBigScreen: isBigScreen,
                )),
            //RIGHT SIDE NEW BOOKS AREA
            Expanded(
                child: Container(
                    margin: _pageStyles.mainContainerPageEdgeInsets(),
                    child: SingleChildScrollView(
                      primary: false,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          /////Desktop Top Bar///////
                          Container(
                            margin: EdgeInsets.only(
                              bottom: 6.0,
                            ),
                            decoration: BoxDecoration(
                              color: Get.isDarkMode
                                  ? Colors.white.withOpacity(0.1)
                                  : Colors.white.withOpacity(0.3),
                              borderRadius: BorderRadius.circular(3),
                              boxShadow: [
                                BoxShadow(
                                  blurRadius: 9,
                                  spreadRadius: 1,
                                  color: Get.isDarkMode
                                      ? Colors.transparent
                                      : Colors.black.withOpacity(0.1),
                                  offset: Offset(3, 3),
                                ),
                              ],
                            ),
                            padding: EdgeInsets.symmetric(
                              horizontal: 21,
                              vertical: 12,
                            ),
                            child:
                                SettingsBarHorizontal(!isSmallScreen, 'Home'),
                          ),

                          //CLASSES CHIPS///
                          Visibility(
                            visible: _classes.isNotEmpty,
                            child: Container(
                              margin: EdgeInsets.only(
                                bottom: 9.0,
                              ),
                              decoration: BoxDecoration(
                                  color: Get.isDarkMode
                                      ? Colors.white.withOpacity(0.1)
                                      : Colors.white.withOpacity(0.3),
                                  borderRadius: BorderRadius.circular(3.0)),
                              padding: EdgeInsets.symmetric(
                                horizontal: 21.0,
                              ),
                              child: _classes.isNotEmpty
                                  ? Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.start,
                                      children: [
                                        Expanded(
                                          child: FormBuilder(
                                            child: FormBuilderChoiceChip(
                                              spacing: 6.0,
                                              runSpacing: 3.0,
                                              decoration: InputDecoration(
                                                border: InputBorder.none,
                                                prefixIcon: Padding(
                                                  padding:
                                                      const EdgeInsets.all(2.0),
                                                  child: Tooltip(
                                                      message:
                                                          'Select Class'.tr,
                                                      child: Icon(
                                                          Icons.class_rounded)),
                                                ),
                                                prefixIconColor: Get.isDarkMode
                                                    ? Get.theme.primaryColorDark
                                                    : Colors.white
                                                        .withOpacity(0.8),
                                              ),
                                              backgroundColor: Get.isDarkMode
                                                  ? Get.theme.primaryColor
                                                  : Colors.blueGrey,
                                              selectedColor: Get.isDarkMode
                                                  ? Colors.white
                                                  : Theme.of(context)
                                                      .primaryColorDark,
                                              initialValue: _classes.firstWhere(
                                                  (element) =>
                                                      element.classId ==
                                                      _classId),
                                              name: 'teacher_class_chip',
                                              onChanged: (value) {
                                                if (value == null) return;
                                                value as TeacherClass;
                                                if (value.classId == _classId)
                                                  return;

                                                try {
                                                  _userController
                                                      .setCurrentClass(value);
                                                  saveSharedCurrentClassId(
                                                      value.classId.toString());
                                                  _schoolAdminController
                                                      .fetchClassDetailsNewByClassId(
                                                          value.classId
                                                              .toString());
                                                  fetchAssignmentData(
                                                      value.classId.toString());
                                                  fetchAssessmentData(
                                                      value.classId.toString());
                                                  box.put(
                                                      CacheManagerKey[
                                                              'currentclass']
                                                          .toString(),
                                                      value.classId);
                                                  if (GetPlatform.isWeb) {
                                                    UHtml.document.cookie =
                                                        "nanhaiV4.8TeacherClassSelected=${value.classId.toString()};path=/;SameSite=Strict;secure";
                                                  }
                                                } catch (error) {
                                                  Log.debug(error);
                                                }
                                              },
                                              options: _classes
                                                  .map((teacherclass) =>
                                                      FormBuilderChipOption<
                                                          TeacherClass>(
                                                        value: teacherclass,
                                                        child: Text(
                                                          teacherclass.className
                                                              .toString(),
                                                          textAlign:
                                                              TextAlign.center,
                                                          style: TextStyle(
                                                            color: Get
                                                                    .isDarkMode
                                                                ? Colors.black
                                                                : Get.theme
                                                                    .primaryColorLight,
                                                            fontSize: 15,
                                                            letterSpacing: 1,
                                                          ),
                                                          overflow:
                                                              TextOverflow.fade,
                                                        ),
                                                      ))
                                                  .toList(),
                                            ),
                                          ),
                                        ),
                                        ////square buttons
                                        Container(
                                          margin: EdgeInsets.symmetric(
                                              vertical: 6.0),
                                          child: Stack(
                                              alignment: Alignment.center,
                                              children: [
                                                Container(
                                                  width: 39,
                                                  height: 39,
                                                  decoration: BoxDecoration(
                                                    color: Get.isDarkMode
                                                        ? Colors.white
                                                            .withOpacity(0.3)
                                                        : Get.theme
                                                            .primaryColorLight
                                                            .withOpacity(0.3),
                                                    borderRadius:
                                                        BorderRadius.circular(
                                                            6),
                                                    boxShadow: [
                                                      BoxShadow(
                                                        blurRadius: 6,
                                                        spreadRadius: 1,
                                                        color: Get.isDarkMode
                                                            ? Colors.transparent
                                                            : Colors.black
                                                                .withOpacity(
                                                                    0.1),
                                                        offset: Offset(3, 3),
                                                      ),
                                                    ],
                                                  ),
                                                ),
                                                Container(
                                                  width: 30,
                                                  height: 30,
                                                  decoration: BoxDecoration(
                                                    color: Get.isDarkMode
                                                        ? Get.theme
                                                            .primaryColorDark
                                                        : Get.theme
                                                            .primaryColorDark
                                                            .withOpacity(0.3),
                                                    borderRadius:
                                                        BorderRadius.circular(
                                                            3),
                                                  ),
                                                ),
                                                Container(
                                                  child: Center(
                                                    child: IconButton(
                                                      icon: const Icon(
                                                        Icons.settings,
                                                      ),
                                                      color: Get.isDarkMode
                                                          ? Colors.black
                                                          : Colors.white
                                                              .withOpacity(0.8),
                                                      onPressed: () {
                                                        String _classId = box
                                                            .get(CacheManagerKey[
                                                                    'currentclass']
                                                                .toString());
                                                        _schoolAdminController
                                                            .fetchClassDetailsNewByClassId(
                                                                _classId);
                                                        Get.to(() =>
                                                            TeacherClassSettings());
                                                      },
                                                    ),
                                                  ),
                                                ),
                                              ]),
                                        ),
                                        Container(
                                          margin: EdgeInsets.symmetric(
                                              vertical: 6.0),
                                          child: Stack(
                                              alignment: Alignment.center,
                                              children: [
                                                Container(
                                                  width: 39,
                                                  height: 39,
                                                  decoration: BoxDecoration(
                                                    color: Get.isDarkMode
                                                        ? Colors.white
                                                            .withOpacity(0.3)
                                                        : Get.theme
                                                            .primaryColorLight
                                                            .withOpacity(0.3),
                                                    borderRadius:
                                                        BorderRadius.circular(
                                                            6),
                                                    boxShadow: [
                                                      BoxShadow(
                                                        blurRadius: 6,
                                                        spreadRadius: 1,
                                                        color: Get.isDarkMode
                                                            ? Colors.transparent
                                                            : Colors.black
                                                                .withOpacity(
                                                                    0.1),
                                                        offset: Offset(3, 3),
                                                      ),
                                                    ],
                                                  ),
                                                ),
                                                Container(
                                                  width: 30,
                                                  height: 30,
                                                  decoration: BoxDecoration(
                                                    color: Get.isDarkMode
                                                        ? Get.theme
                                                            .primaryColorDark
                                                        : Get.theme
                                                            .primaryColorDark
                                                            .withOpacity(0.3),
                                                    borderRadius:
                                                        BorderRadius.circular(
                                                            3),
                                                  ),
                                                ),
                                                Container(
                                                  child: Center(
                                                    child: IconButton(
                                                      icon: const Icon(
                                                        Icons.add_alert,
                                                      ),
                                                      color: Get.isDarkMode
                                                          ? Colors.black
                                                          : Colors.white
                                                              .withOpacity(0.8),
                                                      onPressed: () {
                                                        Get.toNamed(
                                                            '/teacher/class/announcement');
                                                      },
                                                    ),
                                                  ),
                                                ),
                                              ]),
                                        ),
                                      ],
                                    )
                                  : Text(''),
                            ),
                          ),
                          Obx(() {
                            if (_reportsController
                                        .classassignmentsresult.value.list !=
                                    null &&
                                _reportsController.classassignmentsresult.value
                                    .list!.isNotEmpty) {
                              var homeworks = _reportsController
                                  .classassignmentsresult.value.list!
                                  .where((element) => element.status == 'Live')
                                  .toList();
                              int totalFinishedStu = 0;
                              int totalWritingCount = 0;
                              int totalRecordingCount = 0;
                              List<StudentA> unFinishedStudentList = [];
                              BasketItem writingInfo =
                                  homeworks[0].writingInfo ?? new BasketItem();
                              BasketItem recordingInfo =
                                  homeworks[0].recordingInfo ??
                                      new BasketItem();
                              homeworks.forEach((element) {
                                totalFinishedStu +=
                                    element.completedStudents ?? 0;
                                totalRecordingCount +=
                                    element.recordingCount ?? 0;
                                totalWritingCount += element.writingCount ?? 0;
                                unFinishedStudentList
                                    .addAll(element.undoneStudentList ?? []);
                              });
                              return Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Container(
                                      margin: EdgeInsets.fromLTRB(
                                          12.0, 24.0, 0.0, 12.0),
                                      child: Text('HOMEWORK'.tr,
                                          style: TextStyle(
                                            fontFamily: 'Montserrat',
                                            fontSize: 18.0,
                                            color: Get.isDarkMode
                                                ? Get.theme.primaryColorDark
                                                : Get.theme.primaryColorLight,
                                            letterSpacing: 1,
                                          )),
                                    ),
                                    Container(
                                      child: Row(children: [
                                        Expanded(
                                          child: Container(
                                            margin: EdgeInsets.only(right: 6.0),
                                            padding: EdgeInsets.all(12.0),
                                            decoration: BoxDecoration(
                                              color: Get.isDarkMode
                                                  ? AppTheme.colors.darkTheme2
                                                  : Get.theme.primaryColorDark
                                                      .withOpacity(0.1),
                                              borderRadius:
                                                  BorderRadius.circular(
                                                6.0,
                                              ),
                                              boxShadow: [
                                                BoxShadow(
                                                  blurRadius: 1,
                                                  spreadRadius: 1,
                                                  color: Get.isDarkMode
                                                      ? Colors.transparent
                                                      : Colors.black
                                                          .withOpacity(0.1),
                                                  offset: Offset(1, 1),
                                                ),
                                              ],
                                            ),
                                            child: Column(
                                              crossAxisAlignment:
                                                  CrossAxisAlignment.center,
                                              children: [
                                                Container(
                                                  margin: EdgeInsets.only(
                                                      bottom: 3.0),
                                                  padding: EdgeInsets.symmetric(
                                                    horizontal: 12.0,
                                                    vertical: 1.0,
                                                  ),
                                                  decoration: BoxDecoration(
                                                      color: Get.isDarkMode
                                                          ? Colors.black
                                                          : Colors.black
                                                              .withOpacity(0.1),
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                              9.0)),
                                                  child: Text(
                                                    "Submitted".tr,
                                                    maxLines: 1,
                                                    overflow: TextOverflow.fade,
                                                    style: TextStyle(
                                                      fontFamily: 'Montserrat',
                                                      fontSize: 14.0,
                                                      color: Get.isDarkMode
                                                          ? Get.theme
                                                              .primaryColorDark
                                                          : Get.theme
                                                              .primaryColorLight,
                                                    ),
                                                  ),
                                                ),
                                                GestureDetector(
                                                    onTap: () {
                                                      // Get.toNamed(
                                                      //     '/teacher/reports/homework');
                                                      Get.to(() =>
                                                          TeacherReportsHomework(
                                                            showAppbar: true,
                                                          ));
                                                    },
                                                    child: Text(
                                                      totalFinishedStu
                                                          .toString(),
                                                      style: TextStyle(
                                                        decoration:
                                                            TextDecoration
                                                                .underline,
                                                        fontFamily:
                                                            'Montserrat',
                                                        fontSize: 27.0,
                                                        color: Colors.white,
                                                      ),
                                                    )),
                                              ],
                                            ),
                                          ),
                                        ),
                                        Expanded(
                                          child: Container(
                                            margin: EdgeInsets.only(right: 6.0),
                                            padding: EdgeInsets.all(12.0),
                                            decoration: BoxDecoration(
                                              color: Get.isDarkMode
                                                  ? AppTheme.colors.darkTheme2
                                                  : Get.theme.primaryColorDark
                                                      .withOpacity(0.1),
                                              borderRadius:
                                                  BorderRadius.circular(
                                                6.0,
                                              ),
                                              boxShadow: [
                                                BoxShadow(
                                                  blurRadius: 1,
                                                  spreadRadius: 1,
                                                  color: Get.isDarkMode
                                                      ? Colors.transparent
                                                      : Colors.black
                                                          .withOpacity(0.1),
                                                  offset: Offset(1, 1),
                                                ),
                                              ],
                                            ),
                                            child: Column(
                                              crossAxisAlignment:
                                                  CrossAxisAlignment.center,
                                              children: [
                                                Container(
                                                  margin: EdgeInsets.only(
                                                      bottom: 3.0),
                                                  padding: EdgeInsets.symmetric(
                                                    horizontal: 12.0,
                                                    vertical: 1.0,
                                                  ),
                                                  decoration: BoxDecoration(
                                                      color: Get.isDarkMode
                                                          ? Colors.black
                                                          : Colors.black
                                                              .withOpacity(0.1),
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                              9.0)),
                                                  child: Text(
                                                    "Unfinished".tr,
                                                    maxLines: 1,
                                                    overflow: TextOverflow.fade,
                                                    style: TextStyle(
                                                      fontFamily: 'Montserrat',
                                                      fontSize: 14.0,
                                                      color: Get.isDarkMode
                                                          ? Get.theme
                                                              .primaryColorDark
                                                          : Get.theme
                                                              .primaryColorLight,
                                                    ),
                                                  ),
                                                ),
                                                GestureDetector(
                                                    onTap: () {
                                                      showGeneralDialog(
                                                          context: Get.context!,
                                                          transitionDuration:
                                                              Duration(
                                                                  milliseconds:
                                                                      0),
                                                          pageBuilder:
                                                              (bc, ania, anis) {
                                                            return UnfinishedStudentsDialog(
                                                              studentList:
                                                                  unFinishedStudentList,
                                                              isHomework: true,
                                                            );
                                                          });
                                                    },
                                                    child: Text(
                                                      (unFinishedStudentList
                                                              .length)
                                                          .toString(),
                                                      style: TextStyle(
                                                        decoration:
                                                            TextDecoration
                                                                .underline,
                                                        fontFamily:
                                                            'Montserrat',
                                                        fontSize: 27.0,
                                                        color: Colors.white,
                                                      ),
                                                    ))
                                              ],
                                            ),
                                          ),
                                        ),
                                        Expanded(
                                          child: Container(
                                            margin: EdgeInsets.only(right: 6.0),
                                            padding: EdgeInsets.all(12.0),
                                            decoration: BoxDecoration(
                                              color: Get.isDarkMode
                                                  ? AppTheme.colors.darkTheme2
                                                  : Get.theme.primaryColorDark
                                                      .withOpacity(0.1),
                                              borderRadius:
                                                  BorderRadius.circular(
                                                6.0,
                                              ),
                                              boxShadow: [
                                                BoxShadow(
                                                  blurRadius: 1,
                                                  spreadRadius: 1,
                                                  color: Get.isDarkMode
                                                      ? Colors.transparent
                                                      : Colors.black
                                                          .withOpacity(0.1),
                                                  offset: Offset(1, 1),
                                                ),
                                              ],
                                            ),
                                            child: Column(
                                              crossAxisAlignment:
                                                  CrossAxisAlignment.center,
                                              children: [
                                                Container(
                                                  margin: EdgeInsets.only(
                                                      bottom: 3.0),
                                                  padding: EdgeInsets.symmetric(
                                                    horizontal: 12.0,
                                                    vertical: 1.0,
                                                  ),
                                                  decoration: BoxDecoration(
                                                      color: Get.isDarkMode
                                                          ? Colors.black
                                                          : Colors.black
                                                              .withOpacity(0.1),
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                              9.0)),
                                                  child: Text(
                                                    "Recording".tr,
                                                    maxLines: 1,
                                                    overflow: TextOverflow.fade,
                                                    style: TextStyle(
                                                      fontFamily: 'Montserrat',
                                                      fontSize: 14.0,
                                                      color: Get.isDarkMode
                                                          ? Get.theme
                                                              .primaryColorDark
                                                          : Get.theme
                                                              .primaryColorLight,
                                                    ),
                                                  ),
                                                ),
                                                GestureDetector(
                                                    onTap: () {
                                                      if (recordingInfo
                                                              .player !=
                                                          null) {
                                                        // Get.toNamed(
                                                        //     '/teacher/reports/basket');
                                                        Get.to(() =>
                                                            TeacherReportsBasket(
                                                                showAppbar:
                                                                    true,
                                                                filter:
                                                                    'RECORDING'));
                                                      }
                                                    },
                                                    child: Text(
                                                      totalRecordingCount
                                                          .toString(),
                                                      style: TextStyle(
                                                        decoration:
                                                            TextDecoration
                                                                .underline,
                                                        fontFamily:
                                                            'Montserrat',
                                                        fontSize: 27.0,
                                                        color: Colors.white,
                                                      ),
                                                    )),
                                              ],
                                            ),
                                          ),
                                        ),
                                        Expanded(
                                          child: Container(
                                            margin: EdgeInsets.only(right: 6.0),
                                            padding: EdgeInsets.all(12.0),
                                            decoration: BoxDecoration(
                                              color: Get.isDarkMode
                                                  ? AppTheme.colors.darkTheme2
                                                  : Get.theme.primaryColorDark
                                                      .withOpacity(0.1),
                                              borderRadius:
                                                  BorderRadius.circular(
                                                6.0,
                                              ),
                                              boxShadow: [
                                                BoxShadow(
                                                  blurRadius: 1,
                                                  spreadRadius: 1,
                                                  color: Get.isDarkMode
                                                      ? Colors.transparent
                                                      : Colors.black
                                                          .withOpacity(0.1),
                                                  offset: Offset(1, 1),
                                                ),
                                              ],
                                            ),
                                            child: Column(
                                              crossAxisAlignment:
                                                  CrossAxisAlignment.center,
                                              children: [
                                                Container(
                                                  margin: EdgeInsets.only(
                                                      bottom: 3.0),
                                                  padding: EdgeInsets.symmetric(
                                                    horizontal: 12.0,
                                                    vertical: 1.0,
                                                  ),
                                                  decoration: BoxDecoration(
                                                      color: Get.isDarkMode
                                                          ? Colors.black
                                                          : Colors.black
                                                              .withOpacity(0.1),
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                              9.0)),
                                                  child: Text(
                                                    "Writing".tr,
                                                    maxLines: 1,
                                                    overflow: TextOverflow.fade,
                                                    style: TextStyle(
                                                      fontFamily: 'Montserrat',
                                                      fontSize: 14.0,
                                                      color: Get.isDarkMode
                                                          ? Get.theme
                                                              .primaryColorDark
                                                          : Get.theme
                                                              .primaryColorLight,
                                                    ),
                                                  ),
                                                ),
                                                GestureDetector(
                                                    onTap: () {
                                                      if (writingInfo.player !=
                                                          null) {
                                                        Get.to(() =>
                                                            TeacherReportsBasket(
                                                                showAppbar:
                                                                    true,
                                                                filter:
                                                                    'WRITING'));
                                                        // Get.toNamed(
                                                        //     '/teacher/reports/basket');
                                                      }
                                                    },
                                                    child: Text(
                                                      totalWritingCount
                                                          .toString(),
                                                      style: TextStyle(
                                                        decoration:
                                                            TextDecoration
                                                                .underline,
                                                        fontFamily:
                                                            'Montserrat',
                                                        fontSize: 27.0,
                                                        color: Colors.white,
                                                      ),
                                                    )),
                                              ],
                                            ),
                                          ),
                                        )
                                      ]),
                                    ),
                                  ]);
                            } else {
                              return Text('');
                            }
                          }),
                          Obx(
                            () {
                              if (_assessmentsController
                                  .assessAllItems.isNotEmpty) {
                                int totalSubmitted = 0;
                                int totalUngraded = 0;
                                TeacherAssessment assItem =
                                    new TeacherAssessment();
                                List<TeacherAssessment> assItems =
                                    _assessmentsController.assessAllItems
                                        .where((element) =>
                                            element.waiting != null &&
                                            element.waiting! > 0)
                                        .toList();
                                if (assItems.length > 0) {
                                  assItem = assItems[0];
                                }
                                String ungradedId =
                                    assItem.waitingQuestionRecordId ?? '';
                                String assessmentId = assItem.id ?? "";
                                String assessmentName = assItem.name ?? "";
                                // String bookName =
                                //     assItem.questionGroup?.title ?? "";
                                // String assessmentType = assItem.type ?? '';
                                // String userName = assItem.waitingUserName ?? '';

                                List<StudentA> totalUnfinishedStudents = [];
                                _assessmentsController.assessAllItems
                                    .forEach((element) {
                                  totalSubmitted += element.submitted ?? 0;
                                  totalUnfinishedStudents
                                      .addAll(element.undoneStudentList ?? []);
                                  totalUngraded += element.waiting ?? 0;
                                });

                                return Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      Container(
                                        margin: EdgeInsets.fromLTRB(
                                            12.0, 24.0, 0.0, 12.0),
                                        child: Text('ASSESSMENT'.tr,
                                            style: TextStyle(
                                              fontFamily: 'Montserrat',
                                              fontSize: 18.0,
                                              color: Get.isDarkMode
                                                  ? Get.theme.primaryColorDark
                                                  : Get.theme.primaryColorLight,
                                              letterSpacing: 1,
                                            )),
                                      ),
                                      Container(
                                        child: Row(children: [
                                          Expanded(
                                            child: Container(
                                              margin:
                                                  EdgeInsets.only(right: 6.0),
                                              padding: EdgeInsets.all(12.0),
                                              decoration: BoxDecoration(
                                                color: Get.isDarkMode
                                                    ? AppTheme.colors.darkTheme2
                                                    : Get.theme.primaryColorDark
                                                        .withOpacity(0.1),
                                                borderRadius:
                                                    BorderRadius.circular(
                                                  6.0,
                                                ),
                                                boxShadow: [
                                                  BoxShadow(
                                                    blurRadius: 1,
                                                    spreadRadius: 1,
                                                    color: Get.isDarkMode
                                                        ? Colors.transparent
                                                        : Colors.black
                                                            .withOpacity(0.1),
                                                    offset: Offset(1, 1),
                                                  ),
                                                ],
                                              ),
                                              child: Column(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.center,
                                                children: [
                                                  Container(
                                                    margin: EdgeInsets.only(
                                                        bottom: 3.0),
                                                    padding:
                                                        EdgeInsets.symmetric(
                                                      horizontal: 12.0,
                                                      vertical: 1.0,
                                                    ),
                                                    decoration: BoxDecoration(
                                                        color: Get.isDarkMode
                                                            ? Colors.black
                                                            : Colors.black
                                                                .withOpacity(
                                                                    0.1),
                                                        borderRadius:
                                                            BorderRadius
                                                                .circular(9.0)),
                                                    child: Text(
                                                      "Submitted".tr,
                                                      maxLines: 1,
                                                      overflow:
                                                          TextOverflow.fade,
                                                      style: TextStyle(
                                                        fontFamily:
                                                            'Montserrat',
                                                        fontSize: 14.0,
                                                        color: Get.isDarkMode
                                                            ? Get.theme
                                                                .primaryColorDark
                                                            : Get.theme
                                                                .primaryColorLight,
                                                      ),
                                                    ),
                                                  ),
                                                  GestureDetector(
                                                      onTap: () {
                                                        if (assessmentId !=
                                                            '') {
                                                          Get.to(() =>
                                                              SingleAssessmentProgress(
                                                                  assessmentId:
                                                                      assessmentId,
                                                                  name:
                                                                      assessmentName));
                                                        }
                                                      },
                                                      child: Text(
                                                        totalSubmitted
                                                            .toString(),
                                                        style: TextStyle(
                                                          decoration:
                                                              TextDecoration
                                                                  .underline,
                                                          fontFamily:
                                                              'Montserrat',
                                                          fontSize: 27.0,
                                                          color: Colors.white,
                                                        ),
                                                      ))
                                                ],
                                              ),
                                            ),
                                          ),
                                          Expanded(
                                            child: Container(
                                              margin:
                                                  EdgeInsets.only(right: 6.0),
                                              padding: EdgeInsets.all(12.0),
                                              decoration: BoxDecoration(
                                                color: Get.isDarkMode
                                                    ? AppTheme.colors.darkTheme2
                                                    : Get.theme.primaryColorDark
                                                        .withOpacity(0.1),
                                                borderRadius:
                                                    BorderRadius.circular(
                                                  6.0,
                                                ),
                                                boxShadow: [
                                                  BoxShadow(
                                                    blurRadius: 1,
                                                    spreadRadius: 1,
                                                    color: Get.isDarkMode
                                                        ? Colors.transparent
                                                        : Colors.black
                                                            .withOpacity(0.1),
                                                    offset: Offset(1, 1),
                                                  ),
                                                ],
                                              ),
                                              child: Column(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.center,
                                                children: [
                                                  Container(
                                                    margin: EdgeInsets.only(
                                                        bottom: 3.0),
                                                    padding:
                                                        EdgeInsets.symmetric(
                                                      horizontal: 12.0,
                                                      vertical: 1.0,
                                                    ),
                                                    decoration: BoxDecoration(
                                                        color: Get.isDarkMode
                                                            ? Colors.black
                                                            : Colors.black
                                                                .withOpacity(
                                                                    0.1),
                                                        borderRadius:
                                                            BorderRadius
                                                                .circular(9.0)),
                                                    child: Text(
                                                      "Unfinished".tr,
                                                      maxLines: 1,
                                                      overflow:
                                                          TextOverflow.fade,
                                                      style: TextStyle(
                                                        fontFamily:
                                                            'Montserrat',
                                                        fontSize: 14.0,
                                                        color: Get.isDarkMode
                                                            ? Get.theme
                                                                .primaryColorDark
                                                            : Get.theme
                                                                .primaryColorLight,
                                                      ),
                                                    ),
                                                  ),
                                                  GestureDetector(
                                                      onTap: () {
                                                        showGeneralDialog(
                                                            context:
                                                                Get.context!,
                                                            transitionDuration:
                                                                Duration(
                                                                    milliseconds:
                                                                        0),
                                                            pageBuilder: (bc,
                                                                ania, anis) {
                                                              return UnfinishedStudentsDialog(
                                                                studentList:
                                                                    totalUnfinishedStudents,
                                                                isHomework:
                                                                    false,
                                                              );
                                                            });
                                                      },
                                                      child: Text(
                                                        totalUnfinishedStudents
                                                            .length
                                                            .toString(),
                                                        style: TextStyle(
                                                          decoration:
                                                              TextDecoration
                                                                  .underline,
                                                          fontFamily:
                                                              'Montserrat',
                                                          fontSize: 27.0,
                                                          color: Colors.white,
                                                        ),
                                                      ))
                                                ],
                                              ),
                                            ),
                                          ),
                                          Expanded(
                                            child: Container(
                                              margin:
                                                  EdgeInsets.only(right: 6.0),
                                              padding: EdgeInsets.all(12.0),
                                              decoration: BoxDecoration(
                                                color: Get.isDarkMode
                                                    ? AppTheme.colors.darkTheme2
                                                    : Get.theme.primaryColorDark
                                                        .withOpacity(0.1),
                                                borderRadius:
                                                    BorderRadius.circular(
                                                  6.0,
                                                ),
                                                boxShadow: [
                                                  BoxShadow(
                                                    blurRadius: 1,
                                                    spreadRadius: 1,
                                                    color: Get.isDarkMode
                                                        ? Colors.transparent
                                                        : Colors.black
                                                            .withOpacity(0.1),
                                                    offset: Offset(1, 1),
                                                  ),
                                                ],
                                              ),
                                              child: Column(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.center,
                                                children: [
                                                  Container(
                                                    margin: EdgeInsets.only(
                                                        bottom: 3.0),
                                                    padding:
                                                        EdgeInsets.symmetric(
                                                      horizontal: 12.0,
                                                      vertical: 1.0,
                                                    ),
                                                    decoration: BoxDecoration(
                                                        color: Get.isDarkMode
                                                            ? Colors.black
                                                            : Colors.black
                                                                .withOpacity(
                                                                    0.1),
                                                        borderRadius:
                                                            BorderRadius
                                                                .circular(9.0)),
                                                    child: Text(
                                                      "Ungraded".tr,
                                                      maxLines: 1,
                                                      overflow:
                                                          TextOverflow.fade,
                                                      style: TextStyle(
                                                        fontFamily:
                                                            'Montserrat',
                                                        fontSize: 14.0,
                                                        color: Get.isDarkMode
                                                            ? Get.theme
                                                                .primaryColorDark
                                                            : Get.theme
                                                                .primaryColorLight,
                                                      ),
                                                    ),
                                                  ),
                                                  GestureDetector(
                                                      onTap: () {
                                                        if (ungradedId != '') {
                                                          _assessmentsController
                                                              .fetchAssessmentRecordInfoDetails(
                                                                  ungradedId);
                                                          Get.to(() =>
                                                              TeacherReportsAssessment());
                                                          // Get.toNamed(
                                                          //     '/teacher/reports/assessment');
                                                        }
                                                      },
                                                      child: Text(
                                                        totalUngraded
                                                            .toString(),
                                                        style: TextStyle(
                                                          decoration:
                                                              TextDecoration
                                                                  .underline,
                                                          fontFamily:
                                                              'Montserrat',
                                                          fontSize: 27.0,
                                                          color: Colors.white,
                                                        ),
                                                      ))
                                                ],
                                              ),
                                            ),
                                          ),
                                        ]),
                                      ),
                                    ]);
                              } else {
                                return Text("");
                              }
                            },
                          ),
                          Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Container(
                                  margin: EdgeInsets.fromLTRB(
                                      12.0, 24.0, 0.0, 12.0),
                                  child: Text('NEW BOOKS'.tr,
                                      style: TextStyle(
                                        fontFamily: 'Montserrat',
                                        fontSize: 18.0,
                                        color: Get.isDarkMode
                                            ? Get.theme.primaryColorDark
                                            : Get.theme.primaryColorLight,
                                        letterSpacing: 1,
                                      )),
                                ),
                                Container(
                                  width: double.infinity,
                                  height: 310,
                                  child: Obx(() {
                                    if (_booksController
                                        .newBookItems.isNotEmpty)
                                      return BookSliderGridNoBar(
                                          books: _booksController.newBookItems);
                                    else
                                      return Text('');
                                  }),
                                ),
                              ])
                        ],
                      ),
                    ))),
          ],
        ),
      ),
    );
  }
}
